package leetcode;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author Retain
 * @date 2021/8/29 9:39
 */
public class ThreadTest {

    public static void main(String[] args) {

        AtomicInteger integer = new AtomicInteger();
        AtomicInteger num = new AtomicInteger(5);
        // TODO
        System.out.println(num.compareAndSet(5, 1024) + "\t current num" + num.get());
        System.out.println(num.compareAndSet(5, 2019) + "\t current num" + num.get());


        VTest vTest = new VTest();
        new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + "\t come in");
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            vTest.add();
            integer.incrementAndGet();
            integer.incrementAndGet();
            integer.incrementAndGet();
            integer.incrementAndGet();
            integer.incrementAndGet();
            integer.incrementAndGet();
            System.out.println(Thread.currentThread().getName() + "\t come out");
        },"A").start();
        while (true) {
            boolean b = integer.compareAndSet(5, 6);
            if (b) {
                System.out.println(integer.get());
                break;
            }
        }
        while (vTest.num == 0) {
        }
        System.out.println("出来了");
    }



}
class VTest {
    volatile int num = 0;

    public void add() {
        this.num = 10;
    }
}